package cn.com.nes.site.action.crm;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.HttpUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.crm.ClueService;

@Controller("clue")
@RequestMapping("/clue")
public class ClueAction {
	
	@Resource
	private ClueService service;
	
	//查询线索列表
	@RequestMapping(method = RequestMethod.POST, value = "/getClueList")
	public @ResponseBody  PageData getClueList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getClueList(req) ;
			return res;
	}
	//添加线索
	@RequestMapping(method = RequestMethod.POST, value = "/addClue")
	public @ResponseBody  PageData addClue(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.addClue(req) ;
			return res;
	}
	//认领线索
	@RequestMapping(method = RequestMethod.POST, value = "/claimClue")
	public @ResponseBody  PageData claimClue(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.claimClue(req) ;
			return res;
	}
	//查询线索明细
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, value = "/getClueDetail")
	public @ResponseBody  PageData getClueDetail(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getClueDetail(req) ;
			
			Map<String,Object> extData = (Map<String,Object>) res.getExtData();
			if(extData!=null && extData.get("clueInfo")!=null) {
				Map<String,Object> ext = (Map<String,Object>) extData.get("clueInfo");
				//判断图片是存在服务器上面 
				if(ObjectUtil.isNotEmpty(ext.get("picture1")) && !String.valueOf(ext.get("picture1")).startsWith("nesoss/")) {
					String basePath = request.getScheme()+"://"+request.getServerName()+":"+
			                request.getServerPort()+"/wxphoto/";
					ext.put("imgReal1",basePath + ext.get("picture1"));
					ext.put("imgRealBig1",basePath + ext.get("picture1"));
				}
				
			}
			
			return res;
	}		
	//添加跟进
	@RequestMapping(method = RequestMethod.POST, value = "/addLogClaim")
	public @ResponseBody  PageData addLogClaim(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.addLogClaim(req) ;
			return res;
	}
	// 删除跟进
	@RequestMapping(method = RequestMethod.POST, value = "/deleteLogClaim")
	public @ResponseBody  PageData deleteLogClaim(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.deleteLogClaim(req) ;
			return res;
	}
	//添加线索广告
	@RequestMapping(method = RequestMethod.POST, value = "/addClueAdvertisement")
	public @ResponseBody  PageData addClueAdvertisement(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.addClueAdvertisement(req) ;
			return res;
	}		
	//删除线索广告
	@RequestMapping(method = RequestMethod.POST, value = "/deleteClueAdvertisement")
	public @ResponseBody  PageData deleteClueAdvertisement(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.deleteClueAdvertisement(req) ;
			return res;
	}
	//修改线索所有人
	@RequestMapping(method = RequestMethod.POST, value = "/changeClueOwer")
	public @ResponseBody  PageData changeClueOwer(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			if (ObjectUtil.isEmpty(req.getReqData().get("isClick"))){
				res.setRetCode(9);
				res.setRetMessage("线索分配类型不能为空！");
				return res;
			}
			if ("2".equals(req.getReqData().get("isClick"))){//部门
				res.setRetCode(9);
				res.setRetMessage("线索不可分配部门！");
				return res;
			}
			res = service.changeClueOwer(req) ;
			return res;
	}
	//退回线索
	@RequestMapping(method = RequestMethod.POST, value = "/returnClue")
	public @ResponseBody  PageData returnClue(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.returnClue(req) ;
			return res;
	}
	//删除线索
	@RequestMapping(method = RequestMethod.POST, value = "/deleteClue")
	public @ResponseBody  PageData deleteClue(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.deleteClue(req) ;
			return res;
	}		
	//添加广告
	@RequestMapping(method = RequestMethod.POST, value = "/addAdvertisement")
	public @ResponseBody  PageData addAdvertisement(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.addAdvertisement(req) ;
			return res;
	}		
	//删除广告
	@RequestMapping(method = RequestMethod.POST, value = "/deleteAdvertisement")
	public @ResponseBody  PageData deleteAdvertisement(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.deleteAdvertisement(req) ;
			return res;
	}			
	//查询广告列表
	@RequestMapping(method = RequestMethod.POST, value = "/getAdvertisement")
	public @ResponseBody  PageData getAdvertisement(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getAdvertisement(req) ;
			return res;
	}	
	//保存信息到线索池
    @RequestMapping(value = "/saveToClue",method = RequestMethod.POST)
    @ResponseBody
    public PageData saveToClue(HttpServletRequest request,
                                   HttpServletResponse response, ModelMap modelMap){
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        DataReq req = req_Status.getReq() ;
        HashMap<String, Object> reqData = req.getReqData();
        req.setReqData(reqData);
        PageData pageData = service.saveToClue(req);
        return pageData;
    }
	//查询下级公司 部门 树
	@RequestMapping(method = RequestMethod.POST, value = "/getDepartmentTree")
	public @ResponseBody  PageData getDepartmentTree(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getDepartmentTree(req) ;
			return res;
	}
	//获取号码归属地
	@RequestMapping(method = RequestMethod.POST, value = "/getPhoneAddress")
	public @ResponseBody  PageData getPhoneAddress(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq();
			Map<String, String> phoneAddress = HttpUtils.getPhoneAddress((String) req.getReqData().get("phone"));
			res.setExtData(phoneAddress);
			res.setRetCode(0);
			res.setRetMessage("OK");
			return res;
	}

	//查询重复线索
	@RequestMapping(method = RequestMethod.POST, value = "/getClueCheckCnt")
	public @ResponseBody  PageData getClueCheckCnt(HttpServletRequest request,
											   HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		res = service.getClueCheckCnt(req) ;
		return res;
	}

	//删除重复线索
	@RequestMapping(method = RequestMethod.POST, value = "/delCheckClue")
	public @ResponseBody  PageData delCheckClue(HttpServletRequest request,
													   HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		res = service.delCheckClue(req) ;
		return res;
	}

	//批量认领线索
	@RequestMapping(method = RequestMethod.POST, value = "/claimClueMore")
	public @ResponseBody  PageData claimClueMore(HttpServletRequest request,
												HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		res = service.claimClueMore(req) ;
		return res;
	}

	//批量分配线索
	@RequestMapping(method = RequestMethod.POST, value = "/changeClueOwerMore")
	public @ResponseBody  PageData changeClueOwerMore(HttpServletRequest request,
												HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		if (ObjectUtil.isEmpty(req.getReqData().get("isClick"))){
			res.setRetCode(9);
			res.setRetMessage("线索分配类型不能为空！");
			return res;
		}
		if ("2".equals(req.getReqData().get("isClick"))){//部门
			res.setRetCode(9);
			res.setRetMessage("线索不可分配部门！");
			return res;
		}
		res = service.changeClueOwerMore(req) ;
		return res;
	}

}
